<?php

namespace api\controller;

// +----------------------------------------------------------------------
// | Capacity控制层
// +----------------------------------------------------------------------

use common\controller\VerifyController;
use common\exception\LogicException;
use common\service\CapacityService;
use think\Request;

class CapacityController extends VerifyController
{
    /**
     * @var \common\service\CapacityService
     */
    protected $capacityService;

    public function __construct()
    {
        parent::__construct();
        $this->capacityService = new CapacityService();
    }

    /**
     * 写入租户钉钉配置
     * @param Request $request
     * @return mixed
     */
    public function writeTenantDingTalkConfig(Request $request)
    {
        try {
            $param = $request->param();
            $resData =  $this->capacityService->writeTenantDingTalkConfig($param['data']);
            return json(success_response('Tenant dingtalk configuration written successfully.', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 修正指定租户产能表（只能修复当前时间往后的半年的）
     * @param Request $request
     * @return mixed
     */
    public function reviseTenantSchedulingRecord(Request $request)
    {
        try {
            $param = $request->param();
            $resData =  $this->capacityService->reviseTenantSchedulingRecord($param['tenant_id']);
            return json(success_response('Tenant dingtalk configuration written successfully.', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }


    /**
     * 后台配置—保存团队配制
     */
    public function saveCapacityConfig(Request $request)
    {
        $param = $request->param()['data'];

        $id = $param['id'] ?? 0;
        //产能配制字段
        $data = [];
        $data['id'] = intval($id);
        $data['team_category_id'] = $param['team_category_id'];
        $data['name'] = $param['name'];
        $data['type'] = $param['type'];
        $data['default_capacity_num'] = $param['default_capacity_num'];
        $data['unit'] = $param['unit'];
        $data['step_category_id'] = $param['step_category_id'] ?? '';
        $data['attribute'] = $param['attribute'] ?? '';
        //状态1=正常 2=删除
        $data['status'] = 1;
        try {
            $res = $this->capacityService->saveCapacityConfig($data);
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 后台产能配置—列表
     */
    public function getCapacityConfigList(Request $request)
    {
        $param = $request->param()['param'] ?? [];
        $teamCategoryId = $param['team_category_id'] ?? 0;
        $tenantId = $param['tenant_id'] ?? 0;
        try {
            $res = $this->capacityService->getCapacityConfigList($teamCategoryId, $tenantId);
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 后台配置—删除产能配制
     */
    public function delCapacityConfig(Request $request)
    {
        $param = $request->param()['param'];
        try {
            $res = $this->capacityService->delCapacityConfig($param['filter']['ids']);
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 保存租户--产能配制
     */
    public function saveTenantCapacity(Request $request)
    {
        $param = $request->param()['data'];

        $id = $param['id'] ?? 0;
        //产能配制字段
        $data = [];
        $data['id'] = intval($id);
        $data['tenant_id'] = $param['tenant_id'];
        $data['default_capacity_num'] = $param['default_capacity_num'];
        try {
            $res = $this->capacityService->saveTenantCapacity($data);
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 产能导入
     */
    public function importCapacity(Request $request)
    {
        $param = $request->param()['data'];
        try {
            $res = $this->capacityService->importCapacity($param, $request->getTenantId());
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 锁定产能
     */
    public function lockCapacity(Request $request)
    {
        $param = $request->param()['data'];
        try {
            $res = $this->capacityService->lockCapacity($param);
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 获取产能报表
     */
    public function getCapacityReportList(Request $request)
    {
        $param = $request->param()['param'];
        try {
            $res = $this->capacityService->getCapacityReportList($param);
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 保存解决方案
     */
    public function saveCapacityPlan(Request $request)
    {
        $param = $request->param()['data'];
        try {
            $res = $this->capacityService->saveCapacityPlan($param['team_category_id'], $param['report_month'], $param['project_id'], $param['desc']);
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

}
